// src/mixins/clickHandlerMixin.js
export default {
  methods: {
    handleClick(callback, options = {}, ...args) {
      const { needPermission = false, permission = "", debounce = 0 } = options;

      // 1. 权限校验
      if (needPermission && !this.checkPermission(permission)) {
        this.$message.warning("没有操作权限");
        return;
      }

      // 2. 防抖处理
      if (debounce > 0) {
        if (this.clickTimer) clearTimeout(this.clickTimer);
        this.clickTimer = setTimeout(() => {
          this.executeCallback(callback, args);
        }, debounce);
      } else {
        this.executeCallback(callback, args);
      }
    },

    executeCallback(callback, args) {
      if (typeof callback === "function") {
        try {
          callback.apply(this, args);
        } catch (error) {
          console.error("点击回调执行出错：", error);
          this.$message.error("操作失败，请重试");
        }
      } else {
        console.warn("点击回调必须是函数");
      }
    },

    checkPermission(permission) {
      // 实际项目中替换为真实权限校验逻辑
      const userPermissions = this.$store?.state?.user?.permissions || [];
      return userPermissions.includes(permission);
    },
  },

  beforeDestroy() {
    if (this.clickTimer) clearTimeout(this.clickTimer);
  },
};
